#ifndef TRANSFORMERCONTENTCONNECT_HPP_3
#define TRANSFORMERCONTENTCONNECT_HPP_3

#include "logger.hpp"
#include "transContentDefine.h"
#include "transContentDispatcher.hpp"
#include "transContentErrorCode.h"
#include "transContentAssistent.hpp"
#include "transContentIRemove.hpp"
#include "transContentITimerEvent.hpp"

class TransContentConnect: public transContentIRemove, public TransContentITimerEvent, public transContentIDispResponse{
	private:
		int localPort;
		int targetPort;
		int timeout;
		/// state: 0(free, not call connect before), 1(connecting, the connect is going), 2(connecting-stream, the connect is going, and now create the stream record)
		int state;
		int result;
		int canBeRemoved;
		vroute_t vid;
		NodeAddr targetNode;
		connectCallback callback;
		pthread_cond_t waitCond;
		pthread_mutex_t waitMutex;
		ConnectPeer mPeer;
		int remoteBufferSize;
		int remoteStreamStart;

	public:
		TransContentConnect(): localPort(0), targetPort(0), timeout(INT_MAX), state(0), result(0), canBeRemoved(0), vid(-1), callback(NULL),waitCond(PTHREAD_COND_INITIALIZER), waitMutex(PTHREAD_MUTEX_INITIALIZER){
			debug_enter();
			debug_leave();
		}

		virtual ~TransContentConnect(){
			debug_enter();
			debug_leave();
		}

		int getLocalPort() const {
			return this->localPort;
		}


		int canRemoved(){
			return canBeRemoved;
		}

		int getRemoteBufferSize() const{
			return this->remoteBufferSize;
		}

		int getRemoteStreamStart() const {
			return this->remoteStreamStart;
		}

		void setStreamVid(vroute_t vid){
			this->vid = vid;
		}

		void setLocalPort(int port){
			this->localPort = port;
		}

		int getTargetPort() const {
			return this->targetPort;
		}
		void setTargetPort(int port){
			this->targetPort = port;
		}

		void setTargetNode(const NodeAddr* node){
			this->target.serial_id = node->serial_id;
		}
		NodeAddr getTargetNode() const {
			return this->target;
		}

		void setTimeout(int timeout){
			this->timeout = timeout;
		}
		int getTimeout() const {
			return this->timeout;
		}

		void setConnectCallback(connectCallback cccb){
			this->callback = cccb;
		}
		connectCallback getConnectCallback() const {
			return this->cccb;
		}

		///
		/// construct the connect request packet and sent to remote
		///
		int connect();

		////
		///response for connect ack
		///
		int response(char* content, int index);

		void connectFinish();
	private:
		static void* finishRoute(void* p);
		void finishCallback();
		void extractRemoteInfo(char* content);
};
#endif //TRANSFORMERCONTENTCONNECT_HPP_3
